﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using KTVConsole2.Core;
using System.Collections.ObjectModel;

namespace KTVConsole2.UI
{
    /// <summary>
    /// Interaction logic for ReadPlayList.xaml
    /// </summary>
    public partial class ReadPlayList : Window
    {
        Core.KtvEngine s = KTVConsole2.UI.MainWindow.Engine;
        public ReadPlayList()
        {
            InitializeComponent();
            comboBox1.ItemsSource = s.MyPlayList;
            //comboBox1.SelectedIndex = 0;
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            comboBox1.SelectedIndex = comboBox1.Items.Count -1 ;
        }

        private void Window_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.Key)
            {
                case Key.Escape:
                    e.Handled = true;
                    this.Close();
                    break;
            }
        }

        private void comboBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            //if (comboBox1.SelectedItem != null)
            //{
            //    var selected = comboBox1.SelectedItem as FavoriteSongs;
            //    listView1.ItemsSource = selected.Songs;
            //}
        }

        private void btnRead_Click(object sender, RoutedEventArgs e)
        {
            if (comboBox1.SelectedIndex == -1)
            {
                this.Close();
                return;
            }
            var playlist = s.MyPlayList[comboBox1.SelectedIndex].Songs;
            foreach (var a in playlist)
            {
                var songToAdd = a;
                s.AddSongToPlayList(songToAdd);
            }
            this.Close();
        }

        private void btnDelete_Click(object sender, RoutedEventArgs e)
        {
            if (comboBox1.SelectedIndex == -1)
                return;
            s.MyPlayList.RemoveAt(comboBox1.SelectedIndex);
            s.SaveToXML();
            if (comboBox1.HasItems)
                comboBox1.SelectedIndex = 0;
        }

        private void listView1_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            //listView1.Items.RemoveAt(listView1.SelectedIndex);
            if (listView1.SelectedItem == null)
                return;
            ((ObservableCollection<Song>)listView1.ItemsSource).RemoveAt(listView1.SelectedIndex);
            s.SaveToXML();
        }
    }
}
